package Server.ServerCore.Controller;

import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Repository.ChipsServiceImpl;
import Server.ServerCore.Service.DataPacks.packRespose.AllBuyIns_response;
import Server.ServerCore.Service.DataPacks.packRespose.AllIns;
import Server.ServerCore.Service.DataPacks.packRespose.Chips_reback_Response;
import Server.ServerCore.Service.DataPacks.packRespose.SigleBuyIns_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import javolution.util.FastList;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-17
 *         Time: 上午3:21
 *         AllBuyInsController==216
 */
@Service
public class AllBuyInsController extends HandleController {
    private static final long serialVersionUID = -679958940509136327L;
    @Resource
    private ChipsServiceImpl chipsService;
    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {


            double lose=0;
            FastList<AllBuyIns_response>list=new FastList<AllBuyIns_response>();
            for (Integer ID:player.getCircleUnitIDS()){
                CardResultStatusVO cardResultStatusVO=player.getDeckoOfCardsVOFastList()[ID];
                AllBuyIns_response allBuyInsResponse=new AllBuyIns_response();
                allBuyInsResponse.setCircleUnitID(ID);
                allBuyInsResponse.setChips(cardResultStatusVO.getChipsPoints() / 2);
                cardResultStatusVO.setDoubleChips(cardResultStatusVO.getChipsPoints()/2);
                cardResultStatusVO.setInsurance((byte) 1); //下保险flag,赌庄家黑杰克
                lose+=cardResultStatusVO.getChipsPoints()/2;
                list.add(allBuyInsResponse);
            }

            AllIns allIns=new AllIns();
            allIns.setList(list);
            player.DirectSend(allIns.getJsonData(2,16));

            double chips=player.getBargainingChip()-lose;
            player.setBargainingChip(chips);
            PlayerManagerService.update(player.getGameRole(),player.getBargainingChip());

            //返回筹码额
            Chips_reback_Response rebackResponse=new Chips_reback_Response();
            rebackResponse.setChips(player.getBargainingChip());
            player.DirectSend(rebackResponse.getJsonData(2,7));

            if (player.getCircleUnitIDS().get(0)!=5){
                SigleBuyIns_response sigleBuyInsResponse=new SigleBuyIns_response();
                sigleBuyInsResponse.setNextcircleUnitID(player.getCircleUnitIDS().get(0));
                player.setNowCrileUnitID(player.getCircleUnitIDS().get(0));
                player.DirectSend(sigleBuyInsResponse.getJsonData(2,9));
            }
    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[216]=this;
    }
}
